package cn.aitrox.ry.service.auth.controller;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.auth.api.AuthUserRoleApi;
import cn.aitrox.ry.service.auth.dto.user.role.in.AuthUserRoleUpdateInDto;
import cn.aitrox.ry.service.auth.dto.user.role.out.AuthUserRoleDetailDto;
import cn.aitrox.ry.service.auth.service.AuthUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.stream.Collectors;

@RestController
public class AuthUserRoleController implements AuthUserRoleApi {

    @Autowired
    private AuthUserRoleService authUserRoleService;

    @PostMapping(value = "/auth/user/role")
    @Override
    public RespResult update(@RequestBody AuthUserRoleUpdateInDto input) {
        input.setRoleIds(null == input.getRoleIds() ? new ArrayList<>() : input.getRoleIds().stream().distinct().collect(Collectors.toList()));
        return authUserRoleService.update(input.getUserId(), input.getRoleIds());
    }

    @GetMapping(value = "/auth/user/role/{userId}")
    @Override
    public RespResult<AuthUserRoleDetailDto> read(@PathVariable("userId") Integer userId) {
        return authUserRoleService.read(userId);
    }
}
